package com.hsg.lsn13.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

public class ForgroundService extends Service {
    private static final int ID = 10;

    public ForgroundService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(ID,new Notification());

        //如果 18 以上的设备 启动一个Service startForeground给相同的id
        //然后结束这个Service
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            startService(new Intent(this, InnnerService.class));
            Log.d("lsn13","ForgroundService onCreate START InnnerService");
        }
    }

    public static class InnnerService extends Service{

        @Override
        public void onCreate() {
            super.onCreate();
            startForeground(ID,new Notification());
            stopSelf();
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
}
